package com.zhou.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.zhou.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.zhou.gulimall.product.entity.AttrEntity;
import com.zhou.gulimall.product.service.AttrAttrgroupRelationService;
import com.zhou.gulimall.product.service.AttrService;
import com.zhou.gulimall.product.service.CategoryService;
import com.zhou.gulimall.product.vo.WithatteVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zhou.gulimall.product.entity.AttrGroupEntity;
import com.zhou.gulimall.product.service.AttrGroupService;
import com.zhou.common.utils.PageUtils;
import com.zhou.common.utils.R;



/**
 * 属性分组
 *
 * @author Zhouheng
 * @email 87692651@qq.com
 * @date 2022-07-07 13:36:27
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * /product/attrgroup/{attrgroupId}/attr/relation
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
      List<AttrEntity> attrEntityList =  attrService.getRelationAttr(attrgroupId);
      return R.ok().put("data",attrEntityList);
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();
        Long[] path =categoryService.findCatelogIds(catelogId);

        attrGroup.setCatelogIds(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * /product/attrgroup/{attrgroupId}/noattr/relation
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getRelationPage(@RequestParam Map<String, Object> params,
                             @PathVariable("attrgroupId") Long attrGroupId){
        PageUtils page = attrService.getNoRelationAttr(params,attrGroupId);
        return R.ok().put("page",page);
        //return null;
    }


    /**删除当前属性关联
     * /product/attrgroup/attr/relation/delete
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrAttrgroupRelationEntity[] vos){
        attrService.getdetailRelation(vos);
        return R.ok();
    }

    /**
     * 给当前分组添加选中的属性、
     * /product/attrgroup/attr/relation
     */
    @PostMapping("/attr/relation")
    public R attrTrlation(@RequestBody AttrAttrgroupRelationEntity[] relationEntities){
        attrAttrgroupRelationService.updateNullRelation(relationEntities);
        return R.ok();
    }
    /**获取分类下所有分组&关联属性
     * get
     * /product/attrgroup/{catelogId}/withattr
     */
    @GetMapping("/{catelogId}/withattr")
    public R withattr(@PathVariable("catelogId") Long catelogId){
        List<WithatteVo> list = attrGroupService.getWithatteList(catelogId);

        return R.ok().put("data",list);
    }
}
